package io.xiaojl.xj2ee.modules.oss.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import io.xiaojl.xj2ee.common.kit.PageWrapper;
import io.xiaojl.xj2ee.common.xss.SQLFilter;
import io.xiaojl.xj2ee.modules.oss.entity.SysOssEntity;
import io.xiaojl.xj2ee.modules.oss.repository.SysOssRepository;
import io.xiaojl.xj2ee.modules.oss.service.SysOssService;


@Service("sysOssService")
public class SysOssServiceImpl implements SysOssService {
	@Autowired
	private SysOssRepository sysOssRepository;
	
	@Override
	public PageWrapper<SysOssEntity> queryPage(Map<String, Object> params) {
		PageWrapper<SysOssEntity> pw = new PageWrapper<>(params);
		
		String sidx = SQLFilter.sqlInject((String)params.get("sidx"));
        String order = SQLFilter.sqlInject((String)params.get("order"));
        Sort sort = null;
        if(StringUtils.isNotBlank(sidx) && StringUtils.isNotBlank(order)){
        	sort = new Sort("ASC".equalsIgnoreCase(order)?Direction.ASC:Direction.DESC,sidx);
        }
		Pageable pageable = PageRequest.of(pw.getCurrPage(), pw.getPageSize(), sort);
		Page<SysOssEntity> page = sysOssRepository.findAll(pageable);
		pw.setList(page.getContent());
		pw.setTotalCount(Integer.parseInt(page.getTotalElements()+""));
		pw.setTotalPage(page.getTotalPages());
        return pw;
	}

	@Override
	public int insert(SysOssEntity ossEntity) {
		sysOssRepository.save(ossEntity);
		return 1;
	}

	@Override
	public int deleteBatchIds(Long[] ids) {
		List<SysOssEntity> list = sysOssRepository.findAllById(Arrays.asList(ids));
		sysOssRepository.deleteAll(list);
		return list.size();
	}
	
}
